import request from '@/utils/request';

// 查询销售订单的list
export function listOrder(query) {
  const condition = {
    page:query.pageNum,
    count:query.pageSize,
    search:{
      orderId:query.orderId,
      orderStatus: query.orderStatus,
      createTime: query.createTime,
      customerId: query.customerId,
    }
  }
  return request({
    url: '/sales/order/query',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data:  JSON.stringify(condition)
  })
}

// 查询销售订单详情
export function getOrder(orderId) {
  return request({
    url: '/sales/order/' + orderId,
    method: 'get'
  })
}

// 新增销售订单
export function addOrder(data) {
   // 转换 orderDetails 数组中的对象键
  const transformedOrderDetails = data.orderDetails.map(detail => ({
    productId: detail.productId,
    price: detail.price, 
    total: detail.total,
    totalAmount:detail.totalAmount
  }));
  
  const condition = {
    customerId:data.customerId,
    shoppingAddress:data.shoppingAddress,
    salespersonId:data.salespersonId,
    orderDetails:transformedOrderDetails
  }
  return request({
    url: '/sales/order/add',
    method: 'post',
    data: JSON.stringify(condition)
  })
}

// 修改订单信息
export function updateOrder(data) {
  return request({
    url: '/sales/order/update',
    method: 'post',
    data: data
  })
}

// 删除销售订单信息
export function delOrder(orderId) {
  return request({
    url: '/sales/order/' + orderId,
    method: 'delete'
  })
}

//获取销售人员信息
export function getSalesPerson(query){
  const condition = {
    page:query.pageNum,
    count:query.pageSize,
    search:{
      userId:query.id,
      userName: query.name,
      status: query.status
    }
  }
  return request({
    url: '/sales/order/sales',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data:  JSON.stringify(condition)
  })
}

//获取客户信息
export function getCus(query){
  const condition = {
    page:query.pageNum,
    count:query.pageSize,
    search:{
      customerId:query.id,
      customerName: query.name,
      status: query.status
    }
  }
  return request({
    url: '/sales/order/cus',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data:  JSON.stringify(condition)
  })
}

//获取商品列表信息
export function getProducts(query){
  const condition = {
    page:query.pageNum,
    count:query.pageSize,
    search:{
      productId:query.productId,
      productName: query.productName,
      brand: query.brand
    }
  }
  return request({
    url: '/sales/order/products',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data:  JSON.stringify(condition)
  })
}

//获取所有订单详情
export function listOrders(query) {
  const condition = {
    page:query.pageNum,
    count:query.pageSize,
    search:{
      orderId:query.orderId,
      orderStatus: query.orderStatus,
    }
  }
  return request({
    url: '/sales/order/queryAll',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data:  JSON.stringify(condition)
  })
}

//添加退货申请
export function addReturn(query) {
  return request({
    url: '/sales/return/add',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data:  JSON.stringify(query)
  })
}


